//获取应用实例
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    classifyCodes: [],
    classifyIndex: 0,
    sexCodes: ["男", "女"],
    sexCodeIndex: 0,
    linkmanName: '',
    linkmanHeadUrl: app.globalData.headerUrl,
    linkmanSex: '男',
    linkmanAddress: '',
    linkmanTelephone: '',
    linkmanCompany: '',
    linkmanQQ: '',
    linkmanEmail: '',
    classifyGuid: '',
    studentID: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.showNavigationBarLoading();
    getData(that);
  },
  clearInput() {
    this.setData({
      classifyIndex: 0,
      sexCodeIndex: 0,
      linkmanName: '',
      linkmanHeadUrl: '',
      linkmanSex: '男',
      linkmanAddress: '',
      linkmanTelephone: '',
      linkmanCompany: '',
      linkmanQQ: '',
      linkmanEmail: '',
      classifyGuid: '',
      studentID: '',
    });
  },
  submit(){
    var that = this
    wx.showNavigationBarLoading();
    saveLinkman(that);
  },
  bindsexChange: function (e) {
    console.log('picker country 发生选择改变，携带值为', e.detail.value);
    this.setData({
      sexCodeIndex: e.detail.value,
      linkmanSex: this.data.sexCodes[e.detail.value]
    })
  },
  bindclassifyChange: function (e) {
    console.log('picker country 发生选择改变，携带值为', e.detail.value);
    var lists = this.data.list;
    this.setData({
      classifyIndex: e.detail.value,
      classifyGuid: lists[e.detail.value].classifyGuid,
    })
    console.log('classifyGuid', lists[e.detail.value].classifyGuid);
  },
  bindlinkmanNameInput: function (e) {
    this.setData({
      linkmanName: e.detail.value
    })
  },
  bindlinkmanAddressInput: function (e) {
    this.setData({
      linkmanAddress: e.detail.value
    })
  },
  bindlinkmanTelephoneInput: function (e) {
    this.setData({
      linkmanTelephone: e.detail.value
    })
  },
  bindlinkmanCompanyInput: function (e) {
    this.setData({
      linkmanCompany: e.detail.value
    })
  },
  bindlinkmanQQInput: function (e) {
    this.setData({
      linkmanQQ: e.detail.value
    })
  },
  bindlinkmanEmailInput: function (e) {
    this.setData({
      linkmanEmail: e.detail.value
    })
  },
  bindlinkmanQQInput: function (e) {
    this.setData({
      linkmanQQ: e.detail.value
    })
  },
  bindstudentIDInput: function (e) {
    this.setData({
      studentID: e.detail.value
    })
  },
})

//获取数据
var getData = function (self) {
  wx.request({
    url: app.globalData.ServiceUrl + 'classify/getAllclassify', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        index: 1,
        currentPageCount: 200
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      wx.hideNavigationBarLoading();
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        self.setData({
          list: res.data.UserArea.list
        });
        var xiaoquList = res.data.UserArea.list;
        var xiaoquArr = xiaoquList.map(item => {　　　　// 此方法将校区名称区分到一个新数组中
          return item.classifyName;
        });
        self.setData({
          classifyCodes: xiaoquArr,
          classifyGuid: xiaoquList[0].classifyGuid
        });
        console.log('classifyGuid', xiaoquList[0].classifyGuid);
      } else {
        wx.showToast({
          title: '获取数据失败！',
          image: '../../images/img_jinggao.png',
          duration: 1000,
        })
      }
    }
  })
}

var saveLinkman = function (self) {
  if (self.data.linkmanName == '') {
    wx.showToast({
      title: '请输入姓名！',
      image: '../../images/img_jinggao.png',
      duration: 1000
    })
  } else if (self.data.linkmanTelephone == '') {
    wx.showToast({
      title: '请输入手机号码！',
      image: '../../images/img_jinggao.png',
      duration: 1000
    })
  } else if (self.data.studentID == '') {
    wx.showToast({
      title: '请输入学号！',
      image: '../../images/img_jinggao.png',
      duration: 1000
    })
  } else if (self.data.classifyGuid == '') {
    wx.showToast({
      title: '请选择通讯录组！',
      image: '../../images/img_jinggao.png',
      duration: 1000
    })
  } else {
    wx.request({
      url: app.globalData.ServiceUrl + 'Linkman/saveLinkman', //接口地址
      method: 'POST',
      data: {
        ValidateData: app.globalData.ValidateData,
        paras: {
          linkmanName: self.data.linkmanName,
          linkmanHeadUrl: self.data.linkmanHeadUrl,
          linkmanSex: self.data.linkmanSex,
          linkmanAddress: self.data.linkmanAddress,
          linkmanTelephone: self.data.linkmanTelephone,
          linkmanCompany: self.data.linkmanCompany,
          linkmanQQ: self.data.linkmanQQ,
          studentID: self.data.studentID,
          linkmanEmail: self.data.linkmanEmail,
          classifyGuid: self.data.classifyGuid
        }
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        wx.hideNavigationBarLoading();
        if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
          wx.showToast({
            title: '联系人新增成功',
            icon: 'success',
            duration: 2000,
            complete: function () {
              //上传成功，关闭页面
              wx.navigateBack({})
            } //接口调用结束的回调函数  
          })
        }
      }
    })
  }
}